from bs4 import BeautifulSoup
from lxml import etree
from tqdm import tqdm


def parseBasePage(html_content: str) -> list:
    """
    地区列表页的解析器
    :param html_content: 爬取下来的 HTML 字符串
    :return: 获取到的 URL 地址列表
    """
    parse_url_list = []
    soup = BeautifulSoup(html_content, 'html.parser')
    # 找到所有标签
    for a_label in soup.find_all('a', attrs={'target': "_blank"}):
        parse_url_list.append(a_label['href'])
    return parse_url_list[:-4]


def parseRegionPage(html_content: str) -> list:
    """
    每页学校列表的解析器
    :param html_content: HTML 字符串
    :return: 获取到的学校 Dictionary 列表
    """
    result_list = []
    root = etree.HTML(html_content)

    # 找到所有的 table 元素
    for outer_table in tqdm(root.xpath("/html/body/table[@class='tablestyleA']"),
                            desc='extract region page: '):
        school_type = outer_table.xpath("./tr[2]/td[2]/text()")[1].replace(' ', '')

        # 找到内部数据表
        tr_lines = outer_table.xpath("./tr[3]/td/table/tr[position()>1]")

        for line_idx in range(len(tr_lines)):
            if '\t' in tr_lines[line_idx].xpath('./td[1]/text()')[0]:
                # 该行数据是学校网址，并入上一条数据
                # 解析学校网址
                school_url_lst = tr_lines[line_idx].xpath('./td[1]/table/tr/td[3]/a/text()')
                school_url = school_url_lst[-1] if len(school_url_lst) >= 0 else ''
                # 并入当前列表中最后一个 item
                result_list[-1]['school_url'] = school_url

            else:
                # 该行数据是学校基本信息数据，创建新的数据
                # 解析学校名称
                school_name_lst = tr_lines[line_idx].xpath('./td[2]/table/tr[3]/td/text()')
                school_name = school_name_lst[0] if len(school_name_lst) >= 1 else ''
                # 解析学校地址
                school_location_lst = tr_lines[line_idx].xpath('./td[2]/table/tr[4]/td[2]/text()')
                school_location = school_location_lst[0] if len(school_location_lst) > 0 else ''
                # 解析电话与传真
                school_tel_lst = tr_lines[line_idx].xpath('./td[3]/table/tr[1]/td[1]/text()')
                school_tel = school_tel_lst[-1] if len(school_tel_lst) > 1 else ' '
                # 解析校长
                school_master_lst = tr_lines[line_idx].xpath('./td[4]/table/tr[2]/td/text()')
                school_master = (school_master_lst[2].replace('\r\n\t\t\t\t\t\t\t', '')
                                 .replace('\r\n\t\t\t\t\t', '')) if len(school_master_lst) == 3 else ''
                if school_master == '':
                    school_master = (school_master_lst[1].replace('\r\n\t\t\t\t\t', '')
                                     .replace('\r\n\t\t\t\t\t\t\t', ''))

                # 构造 item 的 Dict 类型，并入结果集
                result_list.append({
                    'school_name': school_name,
                    'school_location': school_location,
                    'school_tel': school_tel,
                    'school_master': school_master,
                    'school_type': school_type,
                    'school_url': ''
                })

    return result_list
